package impl;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import java.util.Scanner;

public class Sender {
	private DataInputStream input;
	private DataOutputStream output;
	private Scanner userInput = new Scanner(System.in);
	private boolean connect;
	private static final int NUM_MESSAGES = 25;

	public Sender(String host, int port) {
		try {
			Socket socket = new Socket(host, port);

			System.out.println("Connected to socket " + socket);

			input = new DataInputStream(socket.getInputStream());
			output = new DataOutputStream(socket.getOutputStream());

			SenderThread thread = new SenderThread(this, input);
			thread.start();

			connect = true;

			for(int i=0;i<NUM_MESSAGES;i++) {
				String message = "Test packet " + i;
				processMessage(message);
			}
			
			socket.close();
		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		}
	}

	private void processMessage(String message) {
		try {
			//write the entered message to the output stream that will send the message to the server
			output.writeUTF(message);
		} catch (IOException e) {
			System.out.println(e.getStackTrace());
		}
	}
	
	public boolean isConnect(){
		return connect;
	}
}
